package com.lv.suyiyong.hfr.ui

import android.view.View
import android.view.View.OnClickListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lv.common.base.BaseActivity
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.initSingleClickListener
import com.lv.common.network.model.DataResult
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.Constants
import com.lv.common.utils.MyUtils
import com.lv.common.utils.StatusBarUtil
import com.lv.common.utils.showToast
import com.lv.service.bean.GoodsBean
import com.lv.service.bean.GoodsItemBean
import com.lv.service.model.ServiceViewModel
import com.lv.service.net.BrandHotBody
import com.lv.service.network.BaseYgxRsp
import com.lv.service.network.onBizError
import com.lv.service.network.onBizOK
import com.lv.service.network.onFailure
import com.lv.service.network.onSuccess
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.adapter.HotSalerChildAdapter
import com.lv.suyiyong.hfr.databinding.ActivityHotSalerBrandChildBinding
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 作者： ygx
 * 创建日期：2025/7/21 下午1:36
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：品牌热卖 分类下的商品
 */
@Route(path = ARoutUtils.BrandHotSalerChildActivity)
class BrandHotSalerChildActivity : BaseActivity<ActivityHotSalerBrandChildBinding>(),
    OnClickListener {
    @Autowired(name = "brandid")
    @JvmField
    var mBrandId: String = ""
    private val mTopLeftUrl =
        "https://suyiyong.oss-cn-hangzhou.aliyuncs.com/iOS/projectPic/hfr/icon_brand_hot_sale.png"

    private val mBeans = mutableListOf<GoodsItemBean>()

    private var mAdapter: HotSalerChildAdapter? = null
    private var page: Int = 1
    private var pageSize: Int = Constants.pageSize

    private val viewModel: ServiceViewModel by viewModel()
    private val myScope: CoroutineScope = CoroutineScope(SupervisorJob())

    override fun getLayoutRes() = R.layout.activity_hot_saler_brand_child
    override fun initView() {
        super.initView()
        initStatusBar(this, true, false)
        ARouter.getInstance().inject(this)
        mBinding.apply {
            StatusBarUtil.setHeightAndPadding(this@BrandHotSalerChildActivity, ivTitlePage)
            StatusBarUtil.setHeightAndPadding(this@BrandHotSalerChildActivity, ivBackPage)
            initSingleClickListener(ivBackPage)
        }
    }


    override fun initConfig() {
        super.initConfig()
        mBinding.ivTitlePage.LoadSrc(mTopLeftUrl)
        initAdapter()
        manybrand()
    }


    private fun initAdapter() {
        val gridLayoutManager = GridLayoutManager(this, 3)
        mBinding.rvContent.layoutManager = gridLayoutManager
        mAdapter = HotSalerChildAdapter(mBeans)
        mBinding.rvContent.adapter = mAdapter
//        mBinding.rvContent.addItemDecoration(
//            SpacesItemDecoration(
//                SizeUtils.dp2px(5.0f), SizeUtils.dp2px(5.0f), false
//            )
//        )

    }

    private fun manybrand() {
        startLoading()
        val body = BrandHotBody(page, pageSize, mBrandId, "")
        lifecycleScope.launchWhenCreated {
            val result: DataResult<BaseYgxRsp> =
                myScope.async {
                    viewModel.manybrand(body)
                }
                    .await()
            parseManybrandResult(result)
        }
    }

    private fun parseManybrandResult(data: DataResult<BaseYgxRsp>) {
        loadFinished()
        data.onSuccess {
            onBizOK<GoodsBean> { code, data, message, result ->
                val items = data?.items
                MyUtils.recyclerviewSetData(
                    page, pageSize, mBinding.refreshLayout, mBeans, items, mAdapter
                )
                if (mBeans.isEmpty()) {
                    mAdapter?.setEmptyView(getEmptyCenterView(mBinding.rvContent, "暂无内容"))
                }

            }
            onBizError<Any> { code, message, data ->
                mBinding.refreshLayout.finishRefresh()
                mBinding.refreshLayout.finishLoadMore()
                showToast(message)
            }
        }
        data.onFailure {
            mBinding.refreshLayout.finishRefresh()
            mBinding.refreshLayout.finishLoadMore()
        }
    }

    override fun initEvents() {
        super.initEvents()

        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                manybrand()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                manybrand()
            }
        })

        rigestEventBus()
    }


    private fun rigestEventBus() {

    }

    override fun initData() {
        super.initData()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back_page -> {
                finish()
            }

        }
    }


    override fun onDestroy() {
        super.onDestroy()
//        if (mHintDialog?.isShowing == true) {
//            mHintDialog?.dismiss()
//        }
    }
}